<?php

/*


*/
ini_set ('max_execution_time', 0);

header('Content-Type: text/plain; charset=iso-8859-1');

class Sequencer
{
	public static $seq = array();

	public static function getNext($name)
	{
		if (! isset(Sequencer::$seq[$name]))
			Sequencer::$seq[$name] = 0;
		Sequencer::$seq[$name] = Sequencer::$seq[$name]+1;

		return Sequencer::$seq[$name];
	}

}

class Storage
{
	public static $objects = array();

	public static function createStep()
	{
		$step = new Step();
		$step->id = Sequencer::getNext("Step");
		return $step;
	}

	public static function saveStep($step)
	{
		if (!isset(Storage::$objects["Step"]))
			Storage::$objects["Step"] = array();
		Storage::$objects["Step"][$step->id] = $step;
	}

	public static function createItem($parent)
	{
		$item = new Item();
		$item->id = Sequencer::getNext("Item");
		$item->setParent($parent);
		return $item;
	}

	public static function saveItem($item)
	{
		if (!isset(Storage::$objects["Item"]))
			Storage::$objects["Item"] = array();
		Storage::$objects["Item"][$item->id] = $item;
	}

	public static function buildInsert()
	{
		foreach (Storage::$objects as $arr)
		{
			foreach ($arr as $object)
			{
				$object->buildInsert();
			}
		}
	}
}

class Step
{
	public $id;
	public $label;

	public function buildInsert()
	{
		$label = str_replace("&#39;", "''", html_entity_decode(str_replace("&amp;", "&", $this->label)));
		echo "INSERT INTO step(id,label) VALUES ({$this->id}, '$label');\n";
	}
}

class Item
{
	public $id;
	public $label;
	public $order;
	public $stepId;
	public $parentItemId;

	public function setParent($p)
	{
		if ($p instanceof Step)
		{
			$this->stepId = $p->id;
		}else{
			$this->parentItemId = $p->id;
		}
	}

	public function buildInsert()
	{
		$label = str_replace("&#39;", "''", html_entity_decode(str_replace("&amp;", "&", $this->label)));
		if (isset($this->parentItemId) && $this->parentItemId != null)
			echo "INSERT INTO topic(id,label,parent_id,orderIndex) VALUES ({$this->id}, '$label', {$this->parentItemId}, {$this->order});\n";
		else
			echo "INSERT INTO topic(id,label,step_id,orderIndex) VALUES ({$this->id}, '$label', {$this->stepId}, {$this->order});\n";
	}
}

/*$classOrder = array_flip(array(
"c6",
"c7",
"c2",
"c5",
"c3"
));*/

$classOrder = array(
"c2" => 0,
//"c4" => 1,
"c4 c9" => 1,
"c1" => 2,
"c7" => 3,
"c3" => 4,
"c9 c11" => 4,
"c11 c9" => 4,
"c6" => 0,
"c4" => 1,
"c5" => 2,
"c8" => 3
);

$lastObj = array();


$xml = html_entity_decode(file_get_contents ("Lefilmini.html"),ENT_QUOTES);
$xml = str_replace("&quot;", '"', $xml);
$xml = str_replace("&rsquo;", "'", $xml);

$xml = file_get_contents ("lefil.doc.html");
$xml = str_replace("&rsquo;", "&#39;", $xml);
$xml = str_replace("&", '&amp;', $xml);

$xml = simplexml_load_string('<?xml version="1.0" encoding="iso-8859-1"?>'."\n".$xml);
//print_r($xml);

$xml = $xml->body;
//print_r($xml);

traverse ($xml);

function traverse($xml)
{
	global $classOrder, $lastObj;
	$order = 1;

	/*
	echo "<--------------------->\n";
	print_r($xml->getName()." ".$xml->attributes()->class."\n");
	echo "<--------------------->\n";
	*/

	foreach ($xml as $i=>$v)
	{
		if ($i == "li")
		{
			$c = (string)$v->attributes()->class;
			if (!isset($classOrder[$c]))
			{
				var_dump($v);
				throw new Exception($c . ": Class not found in \$classOrder");
			}

			$idx = $classOrder[$c];
			if ($idx == 0)
			{
				$parent = Storage::createStep();
				$span = $v->span;
				$parent->label = ((string)$span);
				Storage::saveStep($parent);
			}
			else if (!isset($lastObj[$idx-1]))
			{
				var_dump($v);
				var_dump($lastIndex);
				var_dump($classOrder);
				throw new Exception("Parent object of index $idx not found");
			}
			else
			{
				$parent = $lastObj[$idx-1];
				$parent = Storage::createItem($parent);
				$span = $v->span;
				$parent->label = ((string)$span);
				$parent->order = $order++;
				Storage::saveItem($parent);
			}
			$lastObj[$idx] = $parent;

			/*
			print_r($parent);
			print_r($v);
			*/
		}

		if (count($v->children()) > 0)
		{
			traverse($v);
		}
	}
}

Storage::buildInsert();

?>
